package com.strawcom.charmring.ui;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.utils.DensityUtils;
import org.kymjs.kjframe.widget.RoundImageView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout.LayoutParams;

import com.strawcom.charmring.App;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bean.DailySummary;
import com.strawcom.charmring.util.HttpUtil;
import com.strawcom.charmring.util.PreferenceUtil;
import com.strawcom.charmring.view.TargetSettingView;
import com.strawcom.charmring.view.TitleView;

/**
 * @author WangPeng
 */
public class ExerciseSettingsActivity extends BaseActivity {

	private TitleView titleView;
	private TargetSettingView viewTargetSettingEnergy;
	private TargetSettingView viewTargetSettingTime;

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_exercise_settings);
		super.onCreate(savedInstanceState);

		titleView = (TitleView) findViewById(R.id.view_title);
		titleView.setTitleText(R.string.exercise_settings);
		
		RoundImageView imgTitleHead = new RoundImageView(this);
		android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		final int spacing = DensityUtils.dip2px(this, 40);
		
//		params.setMargins(0, 0, spacing / 2, 0);
		params.width = spacing;
		params.height = spacing;
		imgTitleHead.setLayoutParams(params);
		imgTitleHead.setImageResource(R.drawable.head);
		String headUrl = PreferenceUtil.readString(this, C.sp.headurl);
		if (!TextUtils.isEmpty(headUrl)) {
			KJBitmap kjb = KJBitmap.create();
			kjb.display(imgTitleHead, HttpUtil.url_img + headUrl);
		}
		imgTitleHead.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				actionStart(UserInfoActivity.class);
			}
		});
		titleView.setRightView(imgTitleHead);
	}
	
	@Override protected void onResume() {
		super.onResume();
		final int targetEnergy = PreferenceUtil.readInt(context, C.sp.exercise_target_energy, C.exercise.target_settings_energy_min);
		final int targetTime = PreferenceUtil.readInt(context, C.sp.exercise_target_time, C.exercise.target_settings_time_min);
		
		String[] targets = getResources().getStringArray(R.array.exercise_settings_target);
		String[] units = getResources().getStringArray(R.array.exercise_summary_daily_unit);
		viewTargetSettingEnergy = (TargetSettingView) findViewById(R.id.view_target_setting_energy);
		viewTargetSettingEnergy.setValueInfo(targetEnergy, targets[1], units[1], C.exercise.target_settings_energy_min,
				C.exercise.target_settings_energy_max);
		viewTargetSettingTime = (TargetSettingView) findViewById(R.id.view_target_setting_time);
		viewTargetSettingTime.setValueInfo(targetTime, targets[0], units[0], C.exercise.target_settings_time_min,
				C.exercise.target_settings_time_max);
	}

	@Override protected void onPause() {
		super.onPause();
		final int targetTime = viewTargetSettingTime.getValue();
		PreferenceUtil.write(context, C.sp.exercise_target_time, targetTime);
		final int targetEnergy = viewTargetSettingEnergy.getValue();
		PreferenceUtil.write(context, C.sp.exercise_target_energy, targetEnergy);
		
		DailySummary dailySummary = App.i().getDailySummary();
		dailySummary.setTargetTime(targetTime);
		dailySummary.setTargetEnergy(targetEnergy);
		dailySummary.update(dailySummary.getId());
	}

}
